package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.constant.CodeConstant;
import com.zb.entity.*;
import com.zb.mapper.ZjStoreGoodsStockCodeMapper;
import com.zb.service.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 仓库商品库存一物一码表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-01-08
 */
@Service
public class ZjStoreGoodsStockCodeServiceImpl extends ServiceImpl<ZjStoreGoodsStockCodeMapper, ZjStoreGoodsStockCode> implements ZjStoreGoodsStockCodeService {
    @Resource
    private ZjStoreGoodsStockCodeMapper storeGoodsStockCodeMapper;
    @Resource
    private ZjStoreGoodsStockCode02Service storeGoodsStockCode02Service;
    @Resource
    private ZjStoreGoodsStockCode03Service storeGoodsStockCode03Service;
    @Resource
    private ZjStoreGoodsStockCode04Service storeGoodsStockCode04Service;
    @Resource
    private ZjStoreGoodsStockCode05Service storeGoodsStockCode05Service;

    // 根据一物一码校验并获取已出库的一物一码
    @Override
    public List<ZjStoreGoodsStockCode> getByCodes(Long custAccId, List<String> codes) {
        if (CollectionUtil.isEmpty(codes)) {
            return new ArrayList<>();
        }
        return storeGoodsStockCodeMapper.getByCodes(custAccId, codes);
    }

    // 根据一物一码ID获取出入库库存一物一码信息
    @Override
    public List<ZjStoreGoodsStockCode> getByCodeIds(Long custAccId, List<Long> codeIds) {
        if (CollectionUtil.isEmpty(codeIds)) {
            return new ArrayList<>();
        }
        return storeGoodsStockCodeMapper.getByCodeIds(custAccId, codeIds);
    }

    // 保存商品一物一码：包含新增与根据仓库商品库存一物一码ID更新
    @Override
    public void saveOrUpdateBatchById(List<ZjStoreGoodsStockCode> storeGoodsStockCodeList) {
        if (CollectionUtil.isEmpty(storeGoodsStockCodeList)) {
            return;
        }
        List<ZjStoreGoodsStockCode> addCodes = new ArrayList<>();
        List<ZjStoreGoodsStockCode> updateCodes = new ArrayList<>();
        List<ZjStoreGoodsStockCode02> addCode02s = new ArrayList<>();
        List<ZjStoreGoodsStockCode02> updateCode02s = new ArrayList<>();
        List<ZjStoreGoodsStockCode03> addCode03s = new ArrayList<>();
        List<ZjStoreGoodsStockCode03> updateCode03s = new ArrayList<>();
        List<ZjStoreGoodsStockCode04> addCode04s = new ArrayList<>();
        List<ZjStoreGoodsStockCode04> updateCode04s = new ArrayList<>();
        List<ZjStoreGoodsStockCode05> addCode05s = new ArrayList<>();
        List<ZjStoreGoodsStockCode05> updateCode05s = new ArrayList<>();
        ZjStoreGoodsStockCode stockCode = null;
        for (ZjStoreGoodsStockCode goodsStockCode : storeGoodsStockCodeList) {
            stockCode = new ZjStoreGoodsStockCode();
            BeanUtil.copyProperties(goodsStockCode, stockCode);
            if (null == stockCode.getId()) {
                stockCode.setId(IdWorker.getId());
                getData(stockCode, addCodes, addCode02s, addCode03s, addCode04s, addCode05s);
                continue;
            }
            getData(stockCode, updateCodes, updateCode02s, updateCode03s, updateCode04s, updateCode05s);
        }
        if (CollectionUtil.isNotEmpty(addCodes)) this.saveBatch(addCodes);
        if (CollectionUtil.isNotEmpty(updateCodes)) this.updateBatchById(updateCodes);
        if (CollectionUtil.isNotEmpty(addCode02s)) storeGoodsStockCode02Service.saveBatch(addCode02s);
        if (CollectionUtil.isNotEmpty(updateCode02s)) storeGoodsStockCode02Service.updateBatchById(updateCode02s);
        if (CollectionUtil.isNotEmpty(addCode03s)) storeGoodsStockCode03Service.saveBatch(addCode03s);
        if (CollectionUtil.isNotEmpty(updateCode03s)) storeGoodsStockCode03Service.updateBatchById(updateCode03s);
        if (CollectionUtil.isNotEmpty(addCode04s)) storeGoodsStockCode04Service.saveBatch(addCode04s);
        if (CollectionUtil.isNotEmpty(updateCode04s)) storeGoodsStockCode04Service.updateBatchById(updateCode04s);
        if (CollectionUtil.isNotEmpty(addCode05s)) storeGoodsStockCode05Service.saveBatch(addCode05s);
        if (CollectionUtil.isNotEmpty(updateCode05s)) storeGoodsStockCode05Service.updateBatchById(updateCode05s);
    }


    private void getData(ZjStoreGoodsStockCode stockCode, List<ZjStoreGoodsStockCode> stockCodes, List<ZjStoreGoodsStockCode02> stockCode02s,
                         List<ZjStoreGoodsStockCode03> stockCode03s, List<ZjStoreGoodsStockCode04> stockCode04s, List<ZjStoreGoodsStockCode05> stockCode05s) {
        switch (CodeConstant.tableType(stockCode.getId())) {
            case 0:
                stockCodes.add(stockCode);
                return;
            case 1:
                stockCode02s.add(new ZjStoreGoodsStockCode02(stockCode));
                return;
            case 2:
                stockCode03s.add(new ZjStoreGoodsStockCode03(stockCode));
                return;
            case 3:
                stockCode04s.add(new ZjStoreGoodsStockCode04(stockCode));
                return;
            case 4:
                stockCode05s.add(new ZjStoreGoodsStockCode05(stockCode));
        }
    }


    // 根据出库ID获取出库物料的一物一码
    @Override
    public List<ZjStoreGoodsStockCode> getByStoreOutboundId(Long storeOutboundId) {
        return storeGoodsStockCodeMapper.getByStoreOutboundId(storeOutboundId);
    }

    // 根据ID删除仓库商品库存一物一码
    @Override
    public void deleteByIds(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        List<Long> ids = new ArrayList<>();
        List<Long> id02s = new ArrayList<>();
        List<Long> id03s = new ArrayList<>();
        List<Long> id04s = new ArrayList<>();
        List<Long> id05s = new ArrayList<>();
        for (Long id : idList) {
            switch (CodeConstant.tableType(id)) {
                case 0:
                    ids.add(id);
                    continue;
                case 1:
                    id02s.add(id);
                    continue;
                case 2:
                    id03s.add(id);
                    continue;
                case 3:
                    id04s.add(id);
                    continue;
                case 4:
                    id05s.add(id);
            }
        }
        if (CollectionUtil.isNotEmpty(ids)) this.removeByIds(ids);
        if (CollectionUtil.isNotEmpty(id02s)) storeGoodsStockCode02Service.removeByIds(id02s);
        if (CollectionUtil.isNotEmpty(id03s)) storeGoodsStockCode03Service.removeByIds(id03s);
        if (CollectionUtil.isNotEmpty(id04s)) storeGoodsStockCode04Service.removeByIds(id04s);
        if (CollectionUtil.isNotEmpty(id05s)) storeGoodsStockCode05Service.removeByIds(id05s);
    }

    // 根据入库ID获取入库物料的一物一码
    @Override
    public List<ZjStoreGoodsStockCode> getByStoreInboundId(Long inboundId) {
        return storeGoodsStockCodeMapper.getByStoreInboundId(inboundId);
    }

    // 根据仓库ID获取关联的库存一物一码信息
    @Override
    public List<Long> getStoreIdByDelStoreIds(List<Long> storeIds) {
        return storeGoodsStockCodeMapper.getStoreIdByDelStoreIds(storeIds);
    }

    // 根据删除的仓库仓位ID获取关联的库存一物一码信息
    @Override
    public List<Long> getStorePositioinIdByDelStorePositionIds(List<Long> delStorePositionIds) {
        return storeGoodsStockCodeMapper.getStorePositioinIdByDelStorePositionIds(delStorePositionIds);
    }
}
